package com.mark.controller.portal;

import com.google.common.collect.Lists;
import com.mark.common.Const;
import com.mark.common.ServerResponse;
import com.mark.pojo.Category;
import com.mark.service.ICategoryService;
import com.mark.util.JsonUtil;
import com.mark.util.RedisShardedPoolUtil;
import com.mark.vo.CategoryIndexVo;
import com.mark.vo.CategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Choisaaaa on 2018/5/15.
 */
@Controller
@RequestMapping("/category/")
public class CategoryController {
    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping(value = "get_all_category.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getAllCategory(HttpSession session){
        List<CategoryIndexVo> data = Lists.newArrayList();
        ServerResponse<List<CategoryVo>>  responseCategoryList =  iCategoryService.getChildrenParallelCategory(0);//根节点
        List<CategoryVo> categories =  responseCategoryList.getData();
        int length = categories.size() >= 4 ? 4 : categories.size(); //length代表只取出一节种类下的前四个的一级商品种类
        for(int j = 0 ;j < length ;j++){
            CategoryVo category = categories.get(j);
            Integer categoryId = category.getId();

            ServerResponse<List<Integer>> responseCategoryIds= iCategoryService.selectCategoryAndChildrenById(categoryId);//根节点以及孩子节点
            List<Integer> categoryIds = responseCategoryIds.getData();
            List<CategoryVo> categoryList = Lists.newArrayList();
            CategoryIndexVo categoryIndexVo = new CategoryIndexVo();
            for( Integer id :categoryIds){
                Category cat = iCategoryService.selectCategoryById(id);
                if(id != categoryId){
                    CategoryVo categoryVo = new CategoryVo();
                    categoryVo = categoryVo.assembleCategoryVo(cat);
                    categoryList.add(categoryVo);
                    categoryIndexVo.setChildCategoryList(categoryList);
                }else{
                    categoryIndexVo.setFirstCategoryId(categoryId);
                    categoryIndexVo.setFirstCategoryName(cat.getProductCategoryName());
                }
            }
            if(categoryIndexVo.getChildCategoryList() != null){
                int categorySize = categoryIndexVo.getChildCategoryList().size();
                if(categorySize > 5){
                    categoryIndexVo.setIsMore(true);
                    for(int i =  categorySize-1; i > 4 ;i--){
                        categoryIndexVo.getChildCategoryList().remove(i);
                    }
                }
            }
            data.add(categoryIndexVo);
        }
        return ServerResponse.createBySuccess(data);
    }

    @RequestMapping(value = "get_Secondly_category.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getSecondlyCategory(Integer parentCategoryId){
        List<CategoryIndexVo> data = Lists.newArrayList();
        ServerResponse<List<Integer>> responseCategoryIds= iCategoryService.selectCategoryAndChildrenById(parentCategoryId);//根节点以及孩子节点
        List<Integer> categoryIds = responseCategoryIds.getData();
        List<CategoryVo> categoryList = Lists.newArrayList();
        CategoryIndexVo categoryIndexVo = new CategoryIndexVo();
        for(Integer id : categoryIds){
            Category cat = iCategoryService.selectCategoryById(id);
            if(id != parentCategoryId){
                CategoryVo categoryVo = new CategoryVo();
                categoryVo = categoryVo.assembleCategoryVo(cat);
                 categoryList.add(categoryVo);
                categoryIndexVo.setChildCategoryList(categoryList);
            }else{
                categoryIndexVo.setFirstCategoryId(parentCategoryId);
                categoryIndexVo.setFirstCategoryName(cat.getProductCategoryName());
            }
        }
        data.add(categoryIndexVo);
        return ServerResponse.createBySuccess(data);
    }

    @RequestMapping("get_category")
    @ResponseBody
    public ServerResponse<List<Category>> getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId" ,defaultValue = "0") Integer categoryId){
        return iCategoryService.getChildrenParallelCategory(categoryId);
    }


}
